// components/uploader/uploader.js
const app = getApp();
var uploadImage = require('../../utils/uploadImg/uploadImg');
let Authorization = "Bearer " + JSON.parse(wx.getStorageSync('token'))
import __config from '../../config/config'

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  /**
   * 组件的初始数据
   */
  data: {
    fileList: [],
    src: '', //保存后的签名图片地址
  },
  /**
   * 组件的方法列表
   */
  methods: {
    clearImg() {
      this.setData({
        fileList: [],
      })
    },
    afterRead(event) {
      const { file } = event.detail;
      console.log(file);
      let that = this
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      wx.uploadFile({
        url: `${__config.baseURL}/app/ossController/uploadOss`, //服务器上传图片url ,
        filePath: file.url,
        name: 'file',
        formData: {
          'fileHost': 'file',
        },
        header: {
          "content-type": "multipart/form-data",
          'Authorization': Authorization
        },
        success(res) {
          let data = res.data;
          data = JSON.parse(res.data);
          if (data.success) {
            // 上传完成需要更新 fileList
            that.triggerEvent('canvasChang', data.data);
            const { fileList = [] } = that.data.fileList;
            fileList.push({ ...file, url: res.data });
            that.setData({ fileList });
            wx.showToast({
              title: '确认成功',
              duration: 2000,
              mask: true
            })

          } else {
            wx.showToast({
              title: '保存失败，请检查网络',
              duration: 3000,
              mask: true
            })
          }
        },
        fail(err) {
          console.log(err)
        }
      })
    },
  }

})
